package com.hlkj.util.dom4j;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

public class XmlUtils {

	@SuppressWarnings("unchecked")
	public static List<HashMap<String, Object>> Dom2List(String xml)
			throws DocumentException {
		Document document = DocumentHelper.parseText(xml);
		HashMap<String, Object> map = new HashMap<String, Object>();
		List<HashMap<String, Object>> arrayList = new ArrayList<HashMap<String, Object>>();
		if (document == null)
			return arrayList;
		Element root = document.getRootElement();
		for (Iterator iterator = root.elementIterator(); iterator.hasNext();) {
			Element e = (Element) iterator.next();
			List list = e.elements();
			map = new HashMap<String, Object>();
			if (list.size() > 0) {
				map.put(e.getName(), Dom2Map(e));
			} else {
				map.put(e.getName(), e.getText());
			}
			arrayList.add(map);
		}
		System.out.println("Dom2List=" + arrayList);
		return arrayList;
	}

	@SuppressWarnings("unchecked")
	public static Map Dom2Map(Element e) {
		Map map = new HashMap();
		List list = e.elements();
		if (list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				Element iter = (Element) list.get(i);
				List mapList = new ArrayList();

				if (iter.elements().size() > 0) {
					Map m = Dom2Map(iter);
					if (map.get(iter.getName()) != null) {
						Object obj = map.get(iter.getName());
						if (!obj.getClass().getName().equals(
								"java.util.ArrayList")) {
							mapList = new ArrayList();
							mapList.add(obj);
							mapList.add(m);
						}
						if (obj.getClass().getName().equals(
								"java.util.ArrayList")) {
							mapList = (List) obj;
							mapList.add(m);
						}
						map.put(iter.getName(), mapList);
					} else
						map.put(iter.getName(), m);
				} else {
					if (map.get(iter.getName()) != null) {
						Object obj = map.get(iter.getName());
						if (!obj.getClass().getName().equals(
								"java.util.ArrayList")) {
							mapList = new ArrayList();
							mapList.add(obj);
							mapList.add(iter.getText());
						}
						if (obj.getClass().getName().equals(
								"java.util.ArrayList")) {
							mapList = (List) obj;
							mapList.add(iter.getText());
						}
						map.put(iter.getName(), mapList);
					} else
						map.put(iter.getName(), iter.getText());
				}
			}
		} else
			map.put(e.getName(), e.getText());
		return map;
	}

	public static Document createXml() {
		Document doc = DocumentHelper.createDocument();
		Element e = doc.addElement("GmMail");
		e.addElement("template_id").setText("2");
		e.addElement("to_address").setText("test@corp.gm.com");
		e.addElement("to_alias").setText("tom");
		e.addElement("message").setText("XML-未来世界 Message");
		e.addElement("recipient_name").setText("xml-tom");
		e.addElement("send_address").setText("xml-test@163.com");
		e.addElement("sender_comp_name").setText("xml-gm");
		e.addElement("subject").setText("xml-hello guys");
		Element Selecteditems = e.addElement("selected_items");

		Element itemsGroup = Selecteditems.addElement("item_group");
		// Element item = itemsGroup.addElement("item");
		itemsGroup.addElement("item_url").setText("http://china.gm.com");
		itemsGroup.addElement("item_name").setText("gm中文站");
		itemsGroup.addElement("item_desc").setText("gm中文站desc");
		itemsGroup.addElement("item_desc2").setText("gm中文站desc2");
		itemsGroup.addElement("item_desc3").setText("gm中文站desc3");
		itemsGroup.addElement("item_desc4").setText("gm中文站desc4");
		Selecteditems.addElement("items_source").setText("products");
		itemsGroup = Selecteditems.addElement("item_group");
		itemsGroup.addElement("item_url").setText("http://www.gm.com");
		itemsGroup.addElement("item_name").setText("gm");
		itemsGroup.addElement("item_desc").setText("gmdesc");
		Element attachment = e.addElement("attachments");
		Element attachmentGroup = attachment.addElement("attachment-group");
		attachmentGroup.addElement("attachment_url").setText(
				"http://china.gm.com/pics/com.cn.gm/story/frsc.jpg");
		attachmentGroup.addElement("attachment_name").setText("黄晓豫");
		Element attachmentDesc = attachmentGroup.addElement("attachment_desc");
		attachmentDesc.addElement("desc1").setText("attach-desc1");
		attachmentDesc.addElement("desc2").setText("attach-desc2");
		attachmentDesc.addElement("desc3").setText("attach-desc3");
		attachmentDesc.addElement("desc4").setText("attach-desc4");
		attachmentGroup = attachment.addElement("attachment-group");
		attachmentGroup.addElement("attachment_url").setText(
				"http://china.gm.com/pics/com.cn.gm/story/wr.jpg");
		attachmentGroup.addElement("attachment_name").setText("张中汉");
		attachmentDesc = attachmentGroup.addElement("attachment_desc");
		attachmentDesc.addElement("desc1").setText("attach-desc1");
		attachmentDesc.addElement("desc2").setText("attach-desc2");
		attachmentDesc = attachmentGroup.addElement("attachment_desc");
		attachmentDesc.addElement("desc1").setText("attach-desc1");
		attachmentDesc.addElement("desc2").setText("attach-desc2");
		Element descGroup = e.addElement("desc_group");
		descGroup.addElement("desc_item").setText("desc_item_1");
		descGroup.addElement("desc_item").setText("desc_item_2");
		descGroup.addElement("desc_item").setText("desc_item_3");
		descGroup.addElement("desc_item").setText("desc_item_4");
		descGroup.addElement("desc_item").setText("desc_item_5");
		return doc;
	}

	public static void main(String[] args) throws DocumentException {
		Document doc = createXml();
		String strXml = doc.asXML();
		System.out.println(strXml);

		List<HashMap<String, Object>> list = XmlUtils.Dom2List(strXml);

		System.out.println(list.toString());
	}
}
